package com.neusoft.nepb.controller;

import com.neusoft.nepb.common.FileUtils;
import com.neusoft.nepb.common.AlertUtils;
import com.neusoft.nepb.common.SceneUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;

import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.IOException;
import java.util.Map;


public class UserRegisterViewController {
    @FXML
    private TextField txt_phoneNumber,txt_userName,txt_passWord1,txt_passWord2;
    @FXML
    private CheckBox optionMan,optionWoman;
    @FXML
    private ImageView imageView;
    //初始化男女设置
    @FXML
    private void handleOptionMan(ActionEvent event){
        if(optionMan.isSelected())
            optionWoman.setSelected(false);
    }
    @FXML
    private void handleOptionWoman(ActionEvent event){
        if(optionWoman.isSelected())
            optionMan.setSelected(false);
    }
    @FXML
    private void initialize(){
        SceneUtils.setImage(imageView,"/com/neusoft/nepb/image/north.jpg");
    }

    //保存注册用户的信息
    public void saveUser() {
        String phoneNumber = txt_phoneNumber.getText();
        String userName = txt_userName.getText();
        String gender;
        String password = null;
        if (txt_passWord1.getText().equals(txt_passWord2.getText())) {
            password = txt_passWord1.getText();
        }else {
            AlertUtils.showDialog("错误！","两次密码不一致");
            txt_passWord1.clear();
            txt_passWord2.clear();
            txt_passWord1.requestFocus();
            return;
        }
        if(optionMan.isSelected()) {
            gender = "man";
        }
        else if(optionWoman.isSelected()) {
            gender = "woman";
        }
        else{
            AlertUtils.showDialog("错误！","请选择您的性别");
            return;
        }
        com.neusoft.nepb.entity.User u1 = new com.neusoft.nepb.entity.User(phoneNumber, userName, password, gender);
        Map<String, com.neusoft.nepb.entity.User> userMap = FileUtils.readMapObject("user.txt");
        userMap.put(u1.getPhoneNumber(), u1);
        FileUtils.writeMapObject("user.txt", userMap);
        AlertUtils.showDialog("用户注册", "注册成功");
    }

    //定义一个跳转到登录的方法
    @FXML
    private void handleLogin(ActionEvent event) throws IOException {
        SceneUtils.switchTo(event,"/com/neusoft/nepb/view/UserLoginView.fxml");
    }
}
